//
//  LoadMoreTableView.m
//  EGOTableViewPullRefreshDemo
//
//  Created by wanyakun on 13-11-6.
//  Copyright (c) 2013年 The9. All rights reserved.
//

#import "LoadMoreTableView.h"

@interface LoadMoreTableView (Private) <UIScrollViewDelegate>

- (void) config;

@end

@implementation LoadMoreTableView

@synthesize loadMoreView;
@synthesize loadMoreDelegate;
@synthesize canLoadMore;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self config];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self config];
}


- (void)dealloc
{
    self.loadMoreView = nil;
    [delegateInterceptor release];
    delegateInterceptor = nil;
    [super dealloc];
}

# pragma mark - Custom view configuration

- (void) config
{
    /* Message interceptor to intercept scrollView delegate messages */
    delegateInterceptor = [[MessageInterceptor alloc] init];
    delegateInterceptor.middleMan = self;
    delegateInterceptor.receiver = self.delegate;
    super.delegate = (id)delegateInterceptor;
    
    /* Load more view init */
    self.loadMoreView = [[[LoadMoreTableFooterView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height, self.bounds.size.width, self.bounds.size.height)] autorelease];
    self.loadMoreView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    self.loadMoreView.delegate = self;
    [self addSubview:loadMoreView];
    
    self.contentInset = UIEdgeInsetsMake(self.contentInset.top, self.contentInset.left, LOADMORE_AREA_HEIGTH, self.contentInset.right);
}


# pragma mark - View changes

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGRect loadMoreFrame = self.loadMoreView.frame;
    loadMoreFrame.origin.y = self.contentSize.height;
    self.loadMoreView.frame = loadMoreFrame;
    
    self.loadMoreView.hidden = (self.contentSize.height < self.frame.size.height) || !self.canLoadMore;
    self.contentInset = self.loadMoreView.hidden ? UIEdgeInsetsMake(self.contentInset.top, self.contentInset.left, 0, self.contentInset.right) : UIEdgeInsetsMake(self.contentInset.top, self.contentInset.left, LOADMORE_AREA_HEIGTH, self.contentInset.right);
    
}

#pragma mark - Preserving the original behaviour

- (void)setDelegate:(id<UITableViewDelegate>)delegate
{
    if(delegateInterceptor) {
        super.delegate = nil;
        delegateInterceptor.receiver = delegate;
        super.delegate = (id)delegateInterceptor;
    } else {
        super.delegate = delegate;
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    [self.loadMoreView egoRefreshScrollViewDidScroll:scrollView];
    
    // Also forward the message to the real delegate
    if ([delegateInterceptor.receiver
         respondsToSelector:@selector(scrollViewDidScroll:)]) {
        [delegateInterceptor.receiver scrollViewDidScroll:scrollView];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    // Also forward the message to the real delegate
    if ([delegateInterceptor.receiver
         respondsToSelector:@selector(scrollViewDidEndDragging:willDecelerate:)]) {
        [delegateInterceptor.receiver scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
    }
}

- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // Also forward the message to the real delegate
    if ([delegateInterceptor.receiver
         respondsToSelector:@selector(scrollViewWillBeginDragging:)]) {
        [delegateInterceptor.receiver scrollViewWillBeginDragging:scrollView];
    }
}

#pragma mark - LoadMoreTableViewDelegate

- (void)loadMoreTableFooterDidTriggerLoadMore:(LoadMoreTableFooterView *)view
{
    [self.loadMoreDelegate loadMoreTableViewDidTriggerLoadMore:self];
}

@end
